<?php

class UserActions extends Actions {
  
  public function pageLogin(){

    $success = true;

    if($this->param('u') && $this->param('p')) {
      $success = $this->user->login($this->param('u'), $this->param('p'), $this->param('r'));
    }

    $view = new View("login");

    if(!$success) {
      $view->data->error = "Nombre de usuario o clave incorrectos";
    }

    $controller = Controller::getInstance();


    Slot::set('seccion_login', true);
    
    $view->layout("layout_admin");
    $view->show();



    
  }
    
  public function pageRetrieve(){

    $success = true;

    if($this->param('u')) {
      $secretToken = $this->user->retrievePassToken($this->param('u'));
      UserHelper::sendRetrieveMail(
        array(
          'template' => dirname(__FILE__).'/templates/retrieveMail.txt',
          'mail' => $this->param('u'),
          'token' => $secretToken
          )
        );
      $this->redirect(HTMLHelper::baseUrl(1)."page/recupero-clave");
      die();
     }

    $view = new View("retrieve");

    $view->layout("layout_admin");
    $view->show();

    
  }
      
  public function pageReset(){

    $success = true;

    if($this->param('p') && $this->param('p2') && ($this->param('p') == $this->param('p2'))) {
      $secretToken = $this->user->retrievePassToken($this->param('mail'));
      if($secretToken == $this->param('t')){
        $this->user->resetPass($this->param('mail'), $this->param('t'), $this->param('p'));
      } else {
      echo "<script>\n".
           "  alert('ERROR: No pudo modificarse la clave.  Contacte al administrador del sitio.');\n".
           "  top.location.href='".HTMLHelper::baseUrl(1)."';\n".
           "</script>\n";
      die();
      }
      echo "<script>\n".
           "  alert('Su clave ha sido modificada exitosamente.');\n".
           "  top.location.href='".HTMLHelper::baseUrl(1)."';\n".
           "</script>\n";
      die();
     }

    $view = new View("reset");

    $view->layout("layout_admin");
    $view->show();

    
  }

  public function pageLogout(){
    $this->user->logout();

    header("Location: /user/login?r=L3Byb2R1Y3RzL2xpc3Q=");

  }
}
